export default {
  template: `
    <div>
      <div>{{model}}</div>
    <label for="">username</label>
    <input type="text" v-model="model.username" />
      <button type="button" 
              @click="handleLogin">login</button>
</div>
  `,
  data() {
    return {
      model: {}
    }
  },
  methods: {
    handleLogin() {
      if (this.model.username === '') {
        alert('用户名不能为空哦~~')
      }
      this.$store.dispatch('toLogin', {      // dispatch toLogin action
        loginUser: this.model.username,
      }).then(() => {
        let redirectUrl = decodeURIComponent(this.$route.query.redirect || '/')
        console.log(redirectUrl)
        // 跳转到指定的路由
        this.$router.push({
          path: redirectUrl
        })
      }).catch((error) => {
        console.log(error.response.data.message)
      })

    }
  }
}
